# 第三方通道


from django.http import JsonResponse
from django import forms
from django.forms.models import model_to_dict
from web.models import PassageCategory, Passage

from web import utils
from web.apis import module

tools = utils.Utils()
module = module.Module()


# 创建通道分类模型
class PassageCategoryForm(forms.ModelForm):
    class Meta:
        model = PassageCategory
        fields = ['name']


# 创建通道分类
def createPassageCategory(request):
    if request.method == 'POST':
        form = PassageCategoryForm(request.POST)
        if form.is_valid():
            form.save()
            pc = form.instance
            new_pc = model_to_dict(pc)
            new_pc['create_time'] = tools.time_format(pc.create_time)
            return JsonResponse({'code': 200, 'msg': '创建成功', 'data': new_pc})
        else:
            error = tools.Forms_GetError(form)
            return JsonResponse({'code': 400, 'msg': error[1], "data": error[0]})


class PassageForm(forms.ModelForm):
    class Meta:
        model = Passage
        fields = "__all__"


# 创建通道
def createPassage(request):
    if request.method == 'POST':
        form = PassageForm(request.POST, files=request.FILES)
        if form.is_valid():
            form.save()
            p = form.instance
            new_p = model_to_dict(p)
            new_p['create_time'] = tools.time_format(p.create_time)
            new_p['icon'] = p.icon.url
            return JsonResponse({'code': 200, 'msg': '创建成功', 'data': new_p})
        else:
            error = tools.Forms_GetError(form)
            return JsonResponse({'code': 400, 'msg': error[1], "data": error[0]})
        


# 获取通道
def getPassage(request):
    PassageCategoryList = []
    PassageCategorys = PassageCategory.objects.filter(is_show=True)
    for item in PassageCategorys:
        new_item = {
            "PassageCategoryName": item.name,
            "items": [],
            "id": item.id
        }
        new_item['create_time'] = tools.time_format(item.create_time)

        new_Passages = Passage.objects.filter(category=item.id)
        for p in new_Passages:
            new_pitem = model_to_dict(p)
            new_pitem['create_time'] = tools.time_format(p.create_time)
            new_pitem['icon'] = p.icon.url
            new_item['items'].append(new_pitem)
        PassageCategoryList.append(new_item)
    return JsonResponse({'code': 200, 'msg': '获取成功', 'data': PassageCategoryList})




# 删除通道分类
def deletePassageCategory(request):
    if request.method == 'POST':
        id = request.POST.get('id')
        PassageCategory.objects.filter(id=id).delete()
        return JsonResponse({'code': 200, 'msg': '删除成功'})
    else:
        return JsonResponse({'code': 400, 'msg': '请求方法错误'})


# 删除通道
def deletePassage(request):
    if request.method == 'POST':
        id = request.POST.get('id')
        Passage.objects.filter(id=id).delete()
        return JsonResponse({'code': 200, 'msg': '删除成功'})
    else:
        return JsonResponse({'code': 400, 'msg': '请求方法错误'})

